<?php

namespace app\index\controller;

use app\common\enums\ResultEnum;
use app\common\service\UserService;
use app\common\utils\ResultVOUtil;
use think\Request;

class User extends Base
{
    protected $userService;
    
    public function __construct(UserService $userService)
    {
        parent::initialize();
        $this-> userService = $userService;
    }
    
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $param = $request->param();
        
        $where = [];
        
        if(!empty($param)){
            if ( !empty($param['search']) ) {
                
                $where[] = ['user_name|name','like','%'.$param['search'].'%'];
            }
            $agency_id = session('admin.agency_id');
            if(strpos($agency_id, '1')===false){
                $where[] = ['agency_id','in',$agency_id];
            }
            
            $data = $this->userService->index($where,$param['page'],$param['length']);
            if($data){
                return ResultVOUtil::success($data);
            }else{
                return ResultVOUtil::error(ResultEnum::FIND_ERROR);
            }
        }
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $param = $request->param();
        if (!empty($param)) {
            $data = $this->userService->save($param);
            if($data){
                return ResultVOUtil::success('');
            }else{
                return ResultVOUtil::error(ResultEnum::WRITE_ERROR);
            }   
        }
        
    }
    
    /**
     * 查询所有客户
     */
    public function list(){
        $data = $this->userService->list();
        if($data){
            return ResultVOUtil::success($data);
        }else{
            return ResultVOUtil::error(ResultEnum::FIND_ERROR);
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    
    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id = 0)
    {
        $data = $this->userService->delete($id);
        if($data){
            return ResultVOUtil::success('');
        }else{
            return ResultVOUtil::error(ResultEnum::DEL_ERROR);
        }
    }

    public function edit(Request $request){
        $param = $request->param();

        if(!empty($param)){
            $data = $this->userService->edit($param);
            if($data){
                return ResultVOUtil::success($data);
            }else{
                return ResultVOUtil::error(ResultEnum::WRITE_ERROR);
            }
        }
    }
    
    public function userLog(Request $request){
        $param = $request->param();
        if(!empty($param)){
            $data = $this->userService->userLog($param);
            if($data){
                return ResultVOUtil::success($data);
            }else{
                return ResultVOUtil::error(ResultEnum::FIND_ERROR);
            }
        }
        
    }
}
